package com.middlegroup.subjectpay.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.middlegroup.subjectpay.entity.SubjectChannelConfig;
import com.middlegroup.subjectpay.mapper.SubjectChannelConfigMapper;
import com.middlegroup.subjectpay.service.SubjectChannelConfigService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 主体端通道配置表 服务实现类
 * </p>
 *
 * @author pc123
 * @since 2019-01-14
 */
@Service
public class SubjectChannelConfigServiceImpl extends ServiceImpl<SubjectChannelConfigMapper, SubjectChannelConfig> implements SubjectChannelConfigService {

    @Resource
    private SubjectChannelConfigMapper subjectChannelConfigMapper;

    @Resource
    private RedisTemplate redisCacheTemplate;


    @Override
    @Cacheable(value = "subjectpay_config",key = "'system:config'+#p0.toString()+#p1.toString()",unless = "#result eq null")
    public SubjectChannelConfig getConfig(String category, String name) {
        if (StringUtils.isBlank(category) || StringUtils.isBlank(name)){
            return null;
        }
        SubjectChannelConfig config = subjectChannelConfigMapper.getConfig(category,name);
        return config;
    }

    @Override
    public String getConfigString(String category, String name) {
        SubjectChannelConfig subjectChannelConfig = getConfig(category,name);
        if (null == subjectChannelConfig){
            return null;
        }
        return subjectChannelConfig.getContent();
    }

    @Override
    public boolean removeConfig(String category, String name) {

        boolean rt = false;
        //删除redis记录
        rt = redisCacheTemplate.delete("subjectpay_config:"+"system:config"+category+name);
        //删除数据库记录
        if (rt){
            subjectChannelConfigMapper.removeConfig(category,name);
            rt = true;
        }
        return rt;
    }

    @Override
    @CachePut(value = "subjectpay_config",key = "'system:config'+#p0.toString()+#p1.toString()",unless = "#result eq null")
    public String putConfig(String category, String name, String content,String remark) {
        //有则更新、无则put
        int rt = subjectChannelConfigMapper.exist(category,name);
        if (rt == 0){
            //不存在在插入
            SubjectChannelConfig subjectChannelConfig = new SubjectChannelConfig();
            subjectChannelConfig.setCategory(category);
            subjectChannelConfig.setName(name);
            subjectChannelConfig.setContent(content);
            subjectChannelConfigMapper.insert(subjectChannelConfig);
        }else {
            //存在则更新
            subjectChannelConfigMapper.updateConfig(category,name,content);
        }

        return content;
    }

    @Override
    public void putConfigs(List<SubjectChannelConfig> configs) {
        if (!CollectionUtils.isEmpty(configs)){
            for (SubjectChannelConfig subjectChannelConfig : configs){
                putConfig(subjectChannelConfig.getCategory(),subjectChannelConfig.getName(),subjectChannelConfig.getContent(),subjectChannelConfig.getRemark());
            }
        }
    }

    @Override
    public List<SubjectChannelConfig> getConfigs(SubjectChannelConfig subjectChannelConfigFind) {
        EntityWrapper<SubjectChannelConfig> entityWrapper = new EntityWrapper<>();
        entityWrapper.setEntity(subjectChannelConfigFind);
        List<SubjectChannelConfig> list = subjectChannelConfigMapper.selectList(entityWrapper);
        return list;
    }
}
